<?php
/**
 * @version $Id: wikiinclude.php 24 2009-03-29 13:19:58Z der.el.kuku $
 * @package    WikiInclude
 * @subpackage
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Nikolai Plath {@link http://www.easy-joomla.org}
 * @author     Created on 26-Mar-2009
 */

//--No direct access
defined( '_JEXEC' ) or die( '=;)' );

jimport('joomla.application.component.controller');

/**
 * WikiInclude Controller
 *
 * @package    WikiInclude
 * @subpackage Controllers
 */
class WikiIncludesControllerWikiInclude extends WikiIncludesController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'add' , 'edit' );
	}// function

	/**
	 * display the edit form
	 * @return void
	 */
	function edit()
	{
		JRequest::setVar( 'view', 'WikiInclude' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}// function

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		$model = $this->getModel('WikiInclude');

		if ($model->store($post)) {
			$msg = JText::_( 'Greeting Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Greeting' );
		}

		// Check the table in so it can be edited.... we are done with it anyway
		$link = 'index.php?option=com_wikiinclude';
		$this->setRedirect($link, $msg);
	}// function

	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		$model = $this->getModel('WikiInclude');
		if(!$model->delete()) {
			$msg = JText::_( 'Error: One or More Greetings Could not be Deleted' );
		} else {
			$msg = JText::_( 'Greeting(s) Deleted' );
		}

		$this->setRedirect( 'index.php?option=com_wikiinclude', $msg );
	}// function

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_wikiinclude', $msg );
	}// function

	function update()
	{
		$id = JRequest::getInt('id');
		if( ! $id )
		{
			echo 'no id';
			return;
		}
		require_once JPATH_COMPONENT.DS.'helpers'.DS.'includer.php';
		$wikiPage = WikiIncluder::getPage('', $id);

		if( ! $wikiPage->text )
		{
			echo $wikiPage->error;
		}
		else
		{
			/*
			 * Store to DB
			 */
			$model = $this->getModel('WikiInclude');
			$date =& JFactory::getDate()->toMySQL();
			JRequest::setVar('last_updated', $date, 'post', true);
			JRequest::setVar('id', $id, 'post', true);
			JRequest::setVar('title', $wikiPage->title, 'post', true);
			JRequest::setVar('text', $wikiPage->text, 'post', true);
			JRequest::setVar('wikirev', $wikiPage->revno, 'post', true);
			if ($model->store($post))
			{
				echo '<strong style="color: green;">'.JText::_('OK').'</strong>';
			}
			else
			{
				echo '<strong style="color: red;">'.JText::_('ERROR').'</strong>';
			}
		}
	}// function
	
	function preview()
	{
		JRequest::setVar( 'view', 'WikiInclude' );
		JRequest::setVar( 'layout', 'preview'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}//function

}// class